/*
 * LineMarker.java
 *
 * Created on 1 May 2007, 10:30
 *
 */

package gui.markers;

import java.awt.Point;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.Color;

import java.util.ArrayList;

/**
 *
 * @author Slawek Wojcicki
 */
public class LineMarker extends FigureMarker {
    
    private ArrayList points;
    private Line2D line;
    private int lastX, lastY;
    private static int instance_counter = 1;
    
    /**
     * Creates a new instance of StraightLineMarker
     */
    public LineMarker() {
        
        super( "Line" + instance_counter++ );
        points = new ArrayList();
    }
    
    public void drawFigure(Graphics2D g, boolean dragged) {
        
        Point p1;
        Point p2;
        if ( points.size() > 0 ) {
            p1 = (Point)points.get(0);
            
            g.draw( new Line2D.Float(p1.x, p1.y, p1.x, p1.y) );
            
            for (int count = 1; count < points.size(); count ++) {
                p2 = p1;
                p1 = (Point) points.get(count);
                g.setXORMode(Color.white);
                if(!dragged)
                    g.setPaintMode();
                g.draw( new Line2D.Float(p1.x, p1.y, p2.x, p2.y) );
            }
        }
    }
    
    public void mouseDragged(int x, int y, Graphics2D g) {
        
        if (new Point(x, y).distanceSq(lastX, lastY) >= 9.0) {
            
            g.draw( new Line2D.Float(lastX, lastY, x, y) );
            lastX = x;
            lastY = y;
            points.add(new Point(x, y));
        }
    }
    
    public void mousePressed(int x, int y, Graphics2D g) {
        
        lastX = x;
        lastY = y;
        g.draw( new Line2D.Float(lastX, lastY, lastX, lastY) );
        points.add(new Point(x, y));
    }
    
    public boolean mouseReleased(int x, int y, Graphics2D g) {
        
        g.draw( new Line2D.Float(lastX, lastY, lastX, lastY) );
        points.add(new Point(x, y));
        return ( points.size() > 1 );
    }
    
    public boolean contains(Point2D p) {
        
        Point point;
        
        for( int i = 0; i < points.size(); i++ ) {
            point = (Point)points.get( i );
            if (point.distance(p) < 10.0)
                return true;
        }
        return false;
    }
    
    public void move(double x, double y) {
        
        Point point;
        for( int i = 0; i < points.size(); i++ ) {
            point = (Point)points.get( i );
            points.set(i, new Point( (int) (point.getX() + x), (int) (point.getY() + y) ) );
        }
    }
}
